package com.jianglin.controller;

import com.github.pagehelper.PageInfo;
import com.jianglin.model.Userinfo;
import com.jianglin.service.IOwnerService;
import com.jianglin.service.IUserinfoService;
import com.jianglin.util.JsonObject;
import com.jianglin.util.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
/**
 *
 *
 * @author liyijia
 */
@Api(tags = {"用户管理"})
@RestController
@RequestMapping("/userinfo")
public class UserinfoController {


    @Resource
    private IUserinfoService userinfoService;

    @Resource
    private IOwnerService ownerService;

    @Resource
    private PasswordEncoder passwordEncoder;

    @RequestMapping("/queryUserInfoAll")
    public JsonObject<Userinfo> queryUserInfoAll(@RequestParam(defaultValue = "1") Integer page,
                                    @RequestParam(defaultValue = "15") Integer limit,
                                    Userinfo userinfo){
        JsonObject<Userinfo> object=new JsonObject<>();
        PageInfo<Userinfo> pageInfo= userinfoService.findUserinfoAll(page,limit,userinfo);
        object.setCode(0);
        object.setMsg("ok");
        object.setCount(pageInfo.getTotal());
        object.setData(pageInfo.getList());
        return object;
    }

    @ApiOperation(value = "删除")
    @RequestMapping("/deleteByIds")
    public R delete(String  ids){
        String[] list= ids.split(",");
        //遍历遍历进行删除
        for(String id:list){
            Userinfo user = userinfoService.findById(Long.parseLong(id));
            ownerService.deleteOwnerUserByUserName(user.getUsername());
            userinfoService.delete(Long.parseLong(id));
        }
        return R.ok();
    }


    @ApiOperation(value = "新增")
    @RequestMapping("/add")
    public R add(@RequestBody Userinfo userinfo){
        userinfoService.add(userinfo);
        return R.ok();
    }


    @ApiOperation(value = "更新密码")
    @RequestMapping("/update")
    public R update(String oldPwd,String newPwd,Integer id){
        //根据id获取当前的数据记录
        Userinfo user=userinfoService.findById(new Long(id));
        if (passwordEncoder.matches(oldPwd, user.getPassword())) {
            // 加密新密码
            String encodedNewPwd = passwordEncoder.encode(newPwd);
            user.setPassword(encodedNewPwd);
            userinfoService.updateData(user);
            return R.ok();
        } else {
            return R.fail("原密码不正确");
        }
    }

    @ApiOperation(value = "查询分页数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码"),
            @ApiImplicitParam(name = "pageCount", value = "每页条数")
    })
    @GetMapping()
    public JsonObject<Userinfo> findListByPage(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "15") Integer pageCount,
            Userinfo condition) {  // 添加参数接收查询条件

        JsonObject<Userinfo> object = new JsonObject<>();
        List<Userinfo> list = userinfoService.findListByPage(page, pageCount, condition);

        // 手动封装分页信息
        object.setCode(0);
        object.setMsg("ok");
        object.setData(list);
        object.setCount(new PageInfo<>(list).getTotal());
        return object;
    }

    @ApiOperation(value = "id查询")
    @GetMapping("{id}")
    public Userinfo findById(@PathVariable Long id){
        return userinfoService.findById(id);
    }

}
